package com.jinzhu.controller;

import com.jinzhu.pojo.Clazz;
import com.jinzhu.pojo.PageBean;
import com.jinzhu.pojo.Result;
import com.jinzhu.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    @GetMapping
    public Result AllClass1(@RequestParam(required = false) String name,
                           @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                           @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("AllClass 获取分页数据");
        return Result.success(clazzService.AllClass1(name, begin, end, page, pageSize));


    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("delete 删除班级信息数据");
        return clazzService.delete(id)>0?Result.success():Result.error();

    }
    @PostMapping
    public Result add(@RequestBody Clazz clazz) {
        log.info("add 增加班级信息数据");

        return  clazzService.add(clazz)>0?Result.success():Result.error();
    }
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id) {
        log.info("get 查询单个班级信息数据");
        Clazz clazz = clazzService.get(id);
        return clazz==null?Result.error():Result.success(clazz);
    }
    @PutMapping
    public Result update(@RequestBody Clazz clazz) {
        log.info("update 修改班级信息数据");

        return clazzService.update(clazz)>0?Result.success():Result.error();

    }
    @GetMapping("/list")
    public Result AllClass() {
        log.info("AllClass 查询所有班级信息数据");
        List<Clazz> AllClass = clazzService.AllClass();
        return Result.success(AllClass);
    }


}
